<template>
  <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
<!--      <a-form-model-item label="状态" prop="status" >-->
<!--        <a-select placeholder="请选择状态" v-model="form.status">-->
<!--          <a-select-option v-for="(d, index) in statusOptions" :key="index" :value="d.value" >{{ d.label }}</a-select-option>-->
<!--        </a-select>-->
<!--      </a-form-model-item>-->
<!--      <a-form-model-item label="删除标志" prop="delFlag" v-if="formType === 1">-->
<!--        <a-input v-model="form.delFlag" placeholder="请输入删除标志" />-->
<!--      </a-form-model-item>-->
      <a-form-model-item label="店铺名称" prop="storeName" >
        <a-input v-model="form.storeName" placeholder="请输入店铺名称" />
      </a-form-model-item>
      <a-form-model-item label="店长名称" prop="storeLeaderId" >
        <a-select v-model="form.storeLeaderId" @change="handleChange" placeholder="请选择邀请人" :filterOption="filterOption" showSearch>
          <a-select-option v-for="item in superList" :key="item.id">
            {{ item.nickName }}({{ item.userPhone }})
          </a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="店铺图片" prop="storeImg" >
        <file-upload v-model="form.storeImg" type="image"></file-upload>
      </a-form-model-item>
      <a-form-model-item label="商品名称" prop="groupOneName" >
        <a-input v-model="form.groupOneName" placeholder="请输入商品名称" />
      </a-form-model-item>
      <a-form-model-item label="商品图片" prop="groupOneImage" >
        <file-upload v-model="form.groupOneImage" type="image"></file-upload>
      </a-form-model-item>
      <a-form-model-item label="上级店铺" prop="parentStoreId" >
        <a-select v-model="form.parentStoreId" @change="handleChangeStore" placeholder="请选择归属店铺" :filterOption="filterStoreOption" showSearch>
          <a-select-option v-for="item in storeList" :key="item.id">
            {{ item.storeName }}({{ item.storeLeader }}-{{ item.storeLeaderPhone }})
          </a-select-option>
        </a-select>
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" :loading="submitLoading" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>
import {getQdStore, addQdStore, updateQdStore, getListQdStore} from '@/api/system/qdStore'
import {getListQdUser} from '@/api/system/qdUser'

export default {
  name: 'CreateForm',
  props: {
    statusOptions: {
      type: Array,
      required: true
    },
    withdrawalStatusOptions: {
      type: Array,
      required: true
    },
    groupBuyingOptions: {
      type: Array,
      required: true
    }
  },
  components: {
  },
  data () {
    return {
      superList: [],
      storeList: [],
      submitLoading: false,
      formTitle: '',
      // 表单参数
      form: {
        id: null,

        status: null,

        delFlag: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        storeName: null,

        storeLeader: null,

        storeLeaderPhone: null,

        storeLeaderId: null,

        storeLeaderImg: null,

        storeImg: null,

        storeNotice: null,

        withdrawalStatus: null,

        maxWithdrawalAmount: null,

        minWithdrawalAmount: null,

        withdrawalRemark: null,

        reservationStartDate: null,

        reservationEndDate: null,

        saleStartDate: null,

        saleEndDate: null,

        noPayDay: null,

        appreciation: null,

        listingFee: null,

        pushFee: null,

        groupBuying: null,

        groupOneName: null,

        groupOneImage: null,

        groupOneMinPrice: null,

        groupOneMaxPrice: null,

        groupOneGoodId: null,

        groupTwoName: null,

        groupTwoImage: null,

        groupTwoMinPrice: null,

        groupTwoMaxPrice: null,

        groupTwoGoodId: null,

        groupThreeName: null,

        groupThreeImage: null,

        groupThreeMinPrice: null,

        groupThreeMaxPrice: null,

        groupThreeGoodId: null,

        parentStoreId: null

      },
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
        storeLeaderId: [{ required: true, message: '店长不能为空', trigger: 'blur' }],
        storeName: [{ required: true, message: '店铺名称不能为空', trigger: 'blur' }],
        storeImg: [{ required: true, message: '店铺图片不能为空', trigger: 'blur' }],
        groupOneName: [{ required: true, message: '商品名称不能为空', trigger: 'blur' }],
        groupOneImage: [{ required: true, message: '商品图片不能为空', trigger: 'blur' }]
      }
    }
  },
  filters: {
  },
  created () {
  },
  computed: {
  },
  watch: {
  },
  mounted () {
    getListQdUser().then(res => {
      this.superList = res.data
      console.log(res.data)
    })
    getListQdStore().then(res => {
      this.storeList = res.data
    })
  },
  methods: {
    filterOption(inputValue, option) {
      const obj = this.superList.find(e => e.id === option.key)
      return (obj.nickName.indexOf(inputValue) > -1) || (obj.userPhone.indexOf(inputValue) > -1)
    },
    filterStoreOption(inputValue, option) {
      const obj = this.superList.find(e => e.id === option.key)
      return (obj.storeLeader.indexOf(inputValue) > -1) || (obj.storeLeaderPhone.indexOf(inputValue) > -1) || (obj.storeName.indexOf(inputValue) > -1)
    },
    handleChange(val) {
      console.log(val)
    },
    handleChangeStore(val) {
      console.log(val)
    },
    onClose () {
      this.open = false
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.formType = 1
      this.form = {
        id: null,

        status: null,

        delFlag: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        storeName: null,

        storeLeader: null,

        storeLeaderPhone: null,

        storeLeaderId: null,

        storeLeaderImg: null,

        storeImg: null,

        storeNotice: null,

        withdrawalStatus: null,

        maxWithdrawalAmount: null,

        minWithdrawalAmount: null,

        withdrawalRemark: null,

        reservationStartDate: null,

        reservationEndDate: null,

        saleStartDate: null,

        saleEndDate: null,

        noPayDay: null,

        appreciation: null,

        listingFee: null,

        pushFee: null,

        groupBuying: null,

        groupOneName: null,

        groupOneImage: null,

        groupOneMinPrice: null,

        groupOneMaxPrice: null,

        groupOneGoodId: null,

        groupTwoName: null,

        groupTwoImage: null,

        groupTwoMinPrice: null,

        groupTwoMaxPrice: null,

        groupTwoGoodId: null,

        groupThreeName: null,

        groupThreeImage: null,

        groupThreeMinPrice: null,

        groupThreeMaxPrice: null,

        groupThreeGoodId: null,

        parentStoreId: null

      }
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset()
      this.formType = 1
      this.open = true
      this.formTitle = '添加'
      this.getDataList()
    },
    /** 修改按钮操作 */
    handleUpdate (row, ids) {
      this.reset()
      this.formType = 2
      const id = row ? row.id : ids
      getQdStore(id).then(response => {
        this.form = response.data
        this.open = true
        this.formTitle = '修改'
      })
    },
    getDataList(){
      getListQdUser().then(res => {
        this.superList = res.data
        console.log(res.data)
      })
      getListQdStore().then(res => {
        this.storeList = res.data
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id !== undefined && this.form.id !== null) {
            updateQdStore(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            addQdStore(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
